import {Component} from 'react';
import React from 'react';
import {connect} from 'umi';

import HighPerformTable from '@/components/HighPerformTable';
import {Button} from 'antd';
import {PlusOutlined} from '@ant-design/icons';
import APIS from '@/apis';
import Modal from 'antd/lib/modal/Modal';

class SchoolManagerPage extends Component<any, any> {
  actionRef: any;

  constructor(props: any) {
    super(props);
    this.state = {
      modalVisible: false, //对话框的显示
      allSchool: [],
    }
  }

  handleDelete(data: any) {
    return APIS.OrgsAPI.DeleteSchool(data)
      .then(() => {
        return Promise.resolve(true);
      })
      .catch(() => {
        return Promise.resolve(false);
      });
  }

  handleAdd(data: any) {
    return APIS.OrgsAPI.CreateNewSchool(data)
      .then(() => {
        return Promise.resolve(true);
      })
      .catch(() => {
        return Promise.resolve(false);
      });
  }

  handleUpdate(data: any) {
    return APIS.OrgsAPI.UpdateSchool(data)
      .then(() => {
        return Promise.resolve(true);
      })
      .catch(() => {
        return Promise.resolve(false);
      });
  }


  requestGetData(
    params: {
      pageSize?: number;
      current?: number;
    },
    sorter: {
      [key: string]: 'ascend' | 'descend';
    },
    filter: {
      [key: string]: React.ReactText[];
    },
  ) {
    return APIS.OrgsAPI.GetSchoolList({...params, sorter, filter});
  }

  handleChangeState(record: any, state: boolean) {
    return APIS.OrgsAPI.ChangeSchoolState({id: record.id, state: state ? 1 : 0}).then(
      (response) => {
        return Promise.resolve(response.data.state === 1);
      },
    );
  }

  clomns: any = [
    {
      title: '学校名称',
      dataIndex: 'name',
      formItemProps: {
        rules: [
          {
            required: true,
            message: '学校名称必填',
          },
        ],
      }

    },
    {
      title: '学校描述',
      dataIndex: 'description',
      width: "45%",
      render: (item: any) => {
        return <span className="course-editor-description">{item}</span>
      },
      formItemProps: {
        rules: [
          {
            required: false,
            message: '规则名称为必填项',
          },
        ],
      }
    },
    {
      title: '更新时间',
      hideInForm: true,
      dataIndex: 'updatedTime',
      // valueType: 'datatime',
    },
  ];

  handleDataImport() {
  }

  handleDataExport() {
  }

  render() {
    return (
      <div>
        <Modal
          title={this.props.title}
          visible={this.props.modalVisible}
          okText="提交"
          cancelText='取消'
          onOk={this.props.handleAdd}
          onCancel={() => {
            this.setState({modalVisible: false})
          }}
        >

        </Modal>
        <HighPerformTable<any>
          requestGetData={this.requestGetData.bind(this)}
          ref={(ref) => (this.actionRef = ref)}
          columns={this.clomns}
          handleDelete={this.handleDelete.bind(this)}
          handleAdd={this.handleAdd.bind(this)}
          handleChangeState={this.handleChangeState.bind(this)}
          handleUpdate={this.handleUpdate.bind(this)}
          toolBarRender={() => [
            <Button type="primary" onClick={() => this.actionRef.handleModalVisible(true)}>
              <PlusOutlined/> 新增学校
            </Button>,
          ]}
        />
      </div>
    );
  }
}

//() => this.setState({modalVisible:true})
export default connect()(SchoolManagerPage);
